package com.uphone.magicleafteacher.pro.activity;

import android.arch.lifecycle.LifecycleOwner;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

import com.blankj.utilcode.util.SizeUtils;
import com.radish.baselibrary.Intent.IntentData;
import com.radish.baselibrary.navigationbar.DefaultNavigationBar;
import com.radish.framelibrary.banner.BannerAdapter;
import com.radish.framelibrary.banner.BannerView;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;
import com.uphone.magicleafteacher.MyApp;
import com.uphone.magicleafteacher.R;
import com.uphone.magicleafteacher.base.BaseGActivity;
import com.uphone.magicleafteacher.http.ApiService;
import com.uphone.magicleafteacher.http.FailureConsumer;
import com.uphone.magicleafteacher.http.RxSchedulers;
import com.uphone.magicleafteacher.http.SuccessfulConsumer;
import com.uphone.magicleafteacher.util.AudioPlayProgressUtil;
import com.uphone.magicleafteacher.util.CommonUtil;
import com.uphone.magicleafteacher.util.GsonUtils;
import com.uphone.magicleafteacher.view.PlayLoadingView;
import com.uphone.magicleafteacher.vo.VoiceBean;

import butterknife.BindView;
import butterknife.OnClick;

public class VoiceActivity extends BaseGActivity {
    @BindView(R.id.banner)
    BannerView banner;
    @BindView(R.id.playLoadingView)
    PlayLoadingView playLoadingView;
    @BindView(R.id.tv_play)
    TextView tvPlay;
    @IntentData
    private int id;
    private DefaultNavigationBar navigationBar;
    private BannerAdapter bannerAdapter;
    private AudioPlayProgressUtil audioUtil;
    private String audioPath;

    @Override
    protected void initBundle() {

    }

    @Override
    protected int initLayout() {
        return R.layout.activity_voice;
    }

    @Override
    protected void initView() {
        audioUtil = new AudioPlayProgressUtil();
    }

    @Override
    protected void initTitle() {
        navigationBar = initTitle("复述音频");
        setRightNavigationText();
    }


    private void setRightNavigationText() {
        int count = 0;
        if (bannerAdapter != null)
            count = bannerAdapter.getCount();
        int currentPosition = banner.getCurrentPosition();
        if (count != 0)
            currentPosition++;
        SpannableString textSpanned1 = new SpannableString(currentPosition + "/" + count);
        textSpanned1.setSpan(new AbsoluteSizeSpan(SizeUtils.sp2px(11)),
                (currentPosition + "").length(), textSpanned1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        textSpanned1.setSpan(new AbsoluteSizeSpan(SizeUtils.sp2px(15)),
                0, currentPosition + "".length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        textSpanned1.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.themecolor)),
                0, currentPosition + "".length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        navigationBar.setRightTitle(textSpanned1);
    }

    @Override
    protected void initData() {
        MyApp.apiService(ApiService.class)
                .repeatData("" + id)
                .compose(RxSchedulers.io_main())
                .doOnSubscribe(d -> {
                    showLoading();
                })
                .doFinally(() -> {
                    closeLoading();
                })
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from((LifecycleOwner) this)))
                .subscribe(new SuccessfulConsumer() {
                    @Override
                    public void success(String jsonObject) {
                        VoiceBean bean = GsonUtils.getGson().fromJson(jsonObject, VoiceBean.class);
                        if (bean != null && bean.getRepeatData() != null) {
                            audioPath = bean.getRepeatData().getAudioPath();
                            if (bean.getRepeatData().getDataPic() != null && bean.getRepeatData().getDataPic().size() != 0) {
                                String[] images = new String[bean.getRepeatData().getDataPic().size()];
                                for (int i = 0; i < bean.getRepeatData().getDataPic().size(); i++) {
                                    images[i] = CommonUtil.getImageUrl(bean.getRepeatData().getDataPic().get(i));
                                }
                                bannerAdapter = new BannerAdapter(VoiceActivity.this, images);
                                banner.setPointFocusDrawable(new ColorDrawable(Color.TRANSPARENT))
                                        .setPointNormalDrawable(new ColorDrawable(Color.TRANSPARENT))
                                        .setAdapter(bannerAdapter);
                                setRightNavigationText();
                            }
                        }
                    }
                }, new FailureConsumer());
    }

    @Override
    protected void initListener() {
        audioUtil.setProgressListener(new AudioPlayProgressUtil.ProgressListener() {
            @Override
            public void start() {
                playLoadingView.setPlaying(true);
                tvPlay.setText("播放中");
            }

            @Override
            public void stop() {
                super.stop();
                playLoadingView.setPlaying(false);
                tvPlay.setText("播放");
            }
        });
    }

    @OnClick(R.id.playLoadingView)
    public void onViewClicked() {
        if (!playLoadingView.isPlaying()) {
            audioUtil.play(CommonUtil.getVideoUrl(audioPath));
        } else {
            audioUtil.stop();
        }
    }
}
